<?php
namespace plugins\message_push\controller;

use cmf\controller\PluginRestBaseController;
use plugins\message_push\model\AliyunSMS;
use plugins\message_push\model\Applet;
use plugins\message_push\model\Email;
use plugins\message_push\model\GeTui;
use plugins\message_push\model\Official;

/**
* 消息推送接口控制器
*/
class ApiIndexController extends PluginRestBaseController
{

    /**
     * 邮件推送
     */
    public function send_email(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        if(empty($param['email_address'])){
            return api_json_echo(false,'未获取到发送对象邮件地址');
        }
        if(empty($param['title'])){
            return api_json_echo(false,'未获取到邮件标题');
        }
        if(empty($param['message'])){
            return api_json_echo(false,'未获取到邮件内容');
        }
        $email = new Email();
        $email_config = db('email_config')->where('company_id',$param['company_id'])->find();
        if(empty($email_config)){
            return api_json_echo(false,'请先配置邮件信息');
        }
        $res = $email->cmf_send_email( $param['email_address'] , $param['title'] , $param['message'], $email_config );
        if( $res [ 'error' ] == 0 ){
            return api_json_echo(true,$res['message']);
        }
        return api_json_echo(false,$res['message']);
    }

    /**
     * 短信推送
     */
    public function send_sms(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        $aliyunSMS = new AliyunSMS();
        $res = $aliyunSMS->send_sms($param);
        if($res['Code'] == 'OK'){
            return api_json_echo(true,'发送成功');
        }
        return api_json_echo(false,$res['Message']);
    }


    /**
     * 公众号推送
     */
    public function send_official(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        if(empty($param['openid'])){
            return api_json_echo(false,'未获取到openid');
        }
        if(empty($param['template_id'])){
            return api_json_echo(false,'未获取到模板id:template_id');
        }
        if(empty($param['url'])){
            $param['url'] = '';
        }
        if(empty($param['data'])){
            return api_json_echo(false,'未获取到消息内容:data');
        }
        $official = new Official();
        $res = $official->send_official($param);
        if( $res [ 'errcode' ] == 0 ){
            return api_json_echo(true,$res['errmsg']);
        }
        return api_json_echo(false,$res['errmsg']);
    }

    /**
     * 小程序推送
     */
    public function send_applet(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        if(empty($param['openid'])){
            return api_json_echo(false,'未获取到openid');
        }
        if(empty($param['template_id'])){
            return api_json_echo(false,'未获取到模板id:template_id');
        }
        if(empty($param['data'])){
            return api_json_echo(false,'未获取到消息内容:data');
        }
        $applet = new Applet();
        $res = $applet->send_applet($param);
        if( $res [ 'errcode' ] == 0 ){
            return api_json_echo(true,$res['errmsg']);
        }
        return api_json_echo(false,$res['errmsg']);
    }

    /**
     * 个推推送
     */
    public function send_getui(){
        $param = $this->request->param();
        if(empty($param['company_id'])){
            return api_json_echo(false,'未获取到公司信息');
        }
        if(empty($param['cid'])){
            return api_json_echo(false,'未获取到cid');
        }
        if(empty($param['title'])){
            return api_json_echo(false,'未获取到标题:title');
        }
        if(empty($param['content'])){
            return api_json_echo(false,'未获取到消息内容:content');
        }
        $geTui = new GeTui();
        $res = $geTui->send_getui($param);
        if($res[0]==200 && $res[1]['code'] == 0){
            return api_json_echo(true,'推送成功');
        }
        return api_json_echo(false,$res[1]['msg']);
    }

	
}